iT邦幫忙

2024 iThome 鐵人賽

DAY 24
0

設置功能分為主頁面進入的設置,以及對局中右上角進入的設置,主頁面的設置有幾種功能:

  • 棋盤顏色
  • 音量大小
  • 音樂選擇

image


activity_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ab7557"
    tools:context=".setting.Setting">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/black"
        android:gravity="center"
        android:text="設置"
        android:textColor="@color/white"
        android:textSize="30dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline34"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline34"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/redbackground_blackline"
        android:gravity="center"
        android:text="返回"
        android:textColor="@color/white"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toTopOf="@+id/guideline34"
        app:layout_constraintEnd_toStartOf="@+id/guideline33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline37"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.15" />

    <TextView
        android:id="@+id/tv_color"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:gravity="center"
        android:text=""
        android:textColor="@color/black"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toTopOf="@+id/guideline58"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline37" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline58"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4" />

    <TextView
        android:id="@+id/tv_colorTitle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:gravity="center"
        android:text="棋盤顏色"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline37"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline34" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline55"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <TextView
        android:id="@+id/tv_volume"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:gravity="center"
        android:text="音量大小"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline55"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline58" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline56"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.55" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline57"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline84"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.3" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline85"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.7" />

    <TextView
        android:id="@+id/tv_musicTitle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:gravity="center"
        android:text="曲目選擇"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline89"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline56" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline86"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.18" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline87"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.37" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:outlineAmbientShadowColor="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/guideline56"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline55" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline89"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.6" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline90"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.65" />

    <TextView
        android:id="@+id/tv_select_board"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline87"
        app:layout_constraintEnd_toStartOf="@+id/guideline85"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/guideline84"
        app:layout_constraintTop_toTopOf="@+id/guideline86"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/tv_AllMusic"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:gravity="center"
        android:text="所有曲目"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline90"
        app:layout_constraintEnd_toStartOf="@+id/guideline57"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline89" />

    <TextView
        android:id="@+id/tv_SelectMusic"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:gravity="center"
        android:text="已選曲目"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline90"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/guideline57"
        app:layout_constraintTop_toTopOf="@+id/guideline89"
        app:layout_constraintVertical_bias="1.0" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_AllMusic"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline57"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline90">

    </androidx.recyclerview.widget.RecyclerView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_SelectMusic"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline57"
        app:layout_constraintTop_toTopOf="@+id/guideline90" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline63"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline136"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.35" />

    <Button
        android:id="@+id/btn_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/red_left"
        android:gravity="center"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toTopOf="@+id/guideline136"
        app:layout_constraintEnd_toStartOf="@+id/guideline84"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline63" />

    <Button
        android:id="@+id/btn_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/red_right"
        android:gravity="center"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toTopOf="@+id/guideline136"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline85"
        app:layout_constraintTop_toTopOf="@+id/guideline63" />

</androidx.constraintlayout.widget.ConstraintLayout>

main()

其中包括元件綁定、初始化、取得上頁資料

private Button btn_back,btn_left,btn_right;
    private Intent intent;
    private MusicData musicData = new MusicData();
    private ColorData colorData = new ColorData();
    private ArrayList musicList = new ArrayList();
    private ArrayList colorList = new ArrayList();
    private TextView tv_select_board;
    private VolumeChangeReceiver volumeChangeReceiver;
    private SeekBar volumeSeekBar;
    private AudioManager audioManager;
    private IntentFilter filter;
    private int choiceColor=0;
    private RecyclerView recyclerview_AllMusic,recyclerview_SelectMusic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        btn_back = findViewById(R.id.btn_back);
        btn_left = findViewById(R.id.btn_left);
        btn_right = findViewById(R.id.btn_right);
        tv_select_board = findViewById(R.id.tv_select_board);
        volumeSeekBar = findViewById(R.id.seekBar);
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        // 音量調整
        volumeChangeReceiver = new VolumeChangeReceiver(volumeSeekBar);
        filter = new IntentFilter("android.media.VOLUME_CHANGED_ACTION");
        registerReceiver(volumeChangeReceiver, filter);
        volumeSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
        volumeSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        volumeSeekBar.getThumb().setColorFilter(Color.parseColor("#7c0000"), PorterDuff.Mode.SRC_ATOP);
        volumeSeekBar.getProgressDrawable().setColorFilter(Color.parseColor("#7c0000"), PorterDuff.Mode.SRC_ATOP);
        // 曲目選單
        recyclerview_AllMusic = findViewById(R.id.recyclerview_AllMusic);
        recyclerview_SelectMusic = findViewById(R.id.recyclerview_SelectMusic);

        getLastPageData();
        setAllMusicList();
        setSelectMusicList();

        if(musicList == null || musicList.size()==0){
            musicList = new ArrayList();
            musicList.add(0);
        }else{
        }

        if(colorList == null || colorList.size()==0){
            isColorButtonInvisible(btn_left,btn_right);
            tv_select_board.setBackgroundResource(colorData.colorImage[0]);
            colorList = new ArrayList();
            colorList.add(0);
        }else{
            choiceColor = (int)colorList.get(0);
            isColorButtonInvisible(btn_left,btn_right);
            tv_select_board.setBackgroundResource(colorData.colorImage[(int)colorList.get(0)]);
        }

        btn_back.setOnClickListener(v -> {
            intent = new Intent(this, MainActivity.class);
            intent.putIntegerArrayListExtra("musicData",musicList);
            intent.putIntegerArrayListExtra("colorData",colorList);
            startActivity(intent);
            finish();
        });
        btn_left.setOnClickListener(v -> {
            if(choiceColor!=0){
                choiceColor-=1;
                tv_select_board.setBackgroundResource(colorData.colorImage[choiceColor]);
                colorList = new ArrayList();
                colorList.add(choiceColor);
            }
            isColorButtonInvisible(btn_left,btn_right);
        });
        btn_right.setOnClickListener(v -> {
            if(choiceColor!=colorData.colorImage.length-1){
                choiceColor+=1;
                tv_select_board.setBackgroundResource(colorData.colorImage[choiceColor]);
                colorList = new ArrayList();
                colorList.add(choiceColor);
            }
            isColorButtonInvisible(btn_left,btn_right);
        });
        volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_SHOW_UI);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

setAllMusicList()

初始化音樂列表

public void setAllMusicList(){
        MusicListAdapter musicListAdapter = new MusicListAdapter();
        recyclerview_AllMusic.setAdapter(musicListAdapter);
        recyclerview_AllMusic.setLayoutManager(new LinearLayoutManager(this));
    }

setSelectMusicList()

初始化音樂列表

public void setSelectMusicList(){
        SelectMusicListAdapter selectMusicListAdapter = new SelectMusicListAdapter();
        recyclerview_SelectMusic.setAdapter(selectMusicListAdapter);
        recyclerview_SelectMusic.setLayoutManager(new LinearLayoutManager(this));
    }

isColorButtonInvisible()

判斷棋盤顏色選擇

public void isColorButtonInvisible(Button btn_left,Button btn_right){
        if(choiceColor==0){
            btn_left.setVisibility(View.INVISIBLE);
        }else{
            btn_left.setVisibility(View.VISIBLE);
        }
        if(choiceColor==colorData.colorImage.length-1){
            btn_right.setVisibility(View.INVISIBLE);
        }else{
            btn_right.setVisibility(View.VISIBLE);
        }
    }

onKeyDown()

攔截返回鍵事件傳資料回上一頁

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            intent = new Intent(this, MainActivity.class);
            intent.putIntegerArrayListExtra("musicData",musicList);
            intent.putIntegerArrayListExtra("colorData",colorList);
            startActivity(intent);
            finish();
        }
        return false;
    }

還需要添加VolumeChangeReceiver,用來控制音量拉動條

VolumeChangeReceiver

public class VolumeChangeReceiver extends BroadcastReceiver {
    private SeekBar volumeSeekBar;
    public VolumeChangeReceiver(SeekBar seekBar) {
        volumeSeekBar = seekBar;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null && intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {

            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            volumeSeekBar.setProgress(currentVolume);
        }
    }
}

主要的方法就到這裡,下一篇實作兩個adapter及musicList、colorList


上一篇
【DAY 23】activity - Player1ChessGame - move
下一篇
【DAY 25】activity - setting - MusicListAdapter
系列文
基於Firebase整合生成式AI研究開發雙人國際象棋系統(Based on Firebase and AI to research chess system)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言